package net.zdreamer.geracomium.ui.home;

import java.util.Timer;
import java.util.TimerTask;

import com.example.geracomium.R;

import net.zdreamer.geracomium.constant.Constant;
import net.zdreamer.geracomium.utils.window.ViewControlor;
import net.zdreamer.geracomium.utils.window.WindowUtil;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SearchActivity extends Activity implements OnClickListener {
	private RelativeLayout layout;
	private AutoCompleteTextView actv;
	private ImageView chahao;
	private TextView okkkk;
	private static final String[] autoStrs = new String[] { "a", "abc", "abcd",
			"abcde", "ba" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getContentView());
		init();
		initViews();
		initListener();
	}

	protected int getContentView() {
		return R.layout.activity_search;
	}

	protected void init() {
		layout = (RelativeLayout) findViewById(R.id.search_layout2);
		actv = (AutoCompleteTextView) findViewById(R.id.actv_search);
		chahao = (ImageView) findViewById(R.id.chahao1);
		okkkk = (TextView) findViewById(R.id.queding);
		actv.requestFocus();
		actv.setFocusable(true);
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				Message message = handler.obtainMessage();
				message.sendToTarget();
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.showSoftInput(actv, InputMethodManager.RESULT_SHOWN);
				imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
			}
		}, 100);
	}

	Handler handler = new  Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			actv.showDropDown();
//			actv.setOnClickListener(ocl);
		}
	};
	
	OnClickListener ocl = new OnClickListener() {
		@Override
		public void onClick(View v) {
			actv.showDropDown();
		}
	};
	
	protected void initViews() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, autoStrs);
		actv.setAdapter(adapter);
		WindowUtil.getWindow(this);
		actv.setDropDownWidth((WindowUtil.getWindow(this))[0] + 10);
		actv.setDropDownHorizontalOffset(-5);
		ViewControlor.setSize(this, layout, 22, 3, true);
	}

	protected void initListener() {
		chahao.setOnClickListener(this);
		okkkk.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(chahao)) {
			actv.setText("");
		} else {
//			if (actv.getText().toString().trim().equals("")) {
//				Toast.makeText(this, "��������������", Toast.LENGTH_SHORT).show();
//			} else {
				Constant.SEARCH_TEXT = actv.getText().toString().trim();
				ViewControlor.hideKeyboard(this);
				finish();
//			}
		}
	}
}
